<?php
/**
 * 数据层初始和公用
 */
namespace APP\M;


abstract class Model
{
    const S_FAIL = 0;
    const S_SUCCESS = 1;
    const S_EXISTS = 2;
    const S_NOT_EXISTS = 3;
    const S_NOT_EMPTY = 4;

    protected $_email = '';

    public function __construct($email = '')
    {
        if ($email) {
            $this->setEmail($email);
        }
        $this->init();
    }

    public function setEmail($email)
    {
        $email = trim($email);
        if ($email) {
            $this->_email = $email;
        }
    }

    public function init()
    {
    }

    public function getDb()
    {
        return \SF\Registry::get('db');
    }

    
    /**
     * 获取单例model
     */
    public function getModel($name, $email = '')
    {
        $email = $email ? $email : $this->_email;
        $model = 'APP\\M\\'. ucfirst($name);
        $key = 'model_'. $name;
        if (!\SF\Registry::isRegistered($key)) {
            \SF\Registry::set($key, new $model($email));
        }
        return \SF\Registry::get($key);
    }


    /**
     * 获取时间戳
     */
    public function getTimestamp()
    {
        return time();
    }
}